﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4MaterialDelivery : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4MaterialDelivery()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.MaterialDelivery.DoubleClick += F4MaterialDelivery_DoubleClick;
            this.MaterialDelivery.PopupMenuShowing += F4MaterialDelivery_PopupMenuShowing;
            this.f4MaterialDeliveryContextMenu.ItemClicked += f4MaterialDeliveryContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = MaterialDelivery.GetFocusedDataRow();
            frm_Edit_F4MaterialDelivery editForm = new frm_Edit_F4MaterialDelivery();
            editForm.ID = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                //LoadData();
            }
            else
            {
                editForm.Dispose();
                MessageBox.Show("Cannot Edit selected record");
            }
        }

        void f4MaterialDeliveryContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4MaterialDeliveryContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4MaterialDelivery add_Form = new frm_Add_F4MaterialDelivery();
                    add_Form.ShowDialog();
                    //LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F4MaterialDelivery_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4MaterialDeliveryContextMenu.Show(MousePosition);
        }

        private void F4MaterialDelivery_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT [ID], [DeliveryDate], [StaffID], [MaterialPart], [DeliveryQuantity], [NotShipping],"
                + " [DeliveryDateTT], [InvoiceID], [Remark]"
                + " FROM [444_MaterialF4Delivery]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF4MaterialDelivery.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("DeliveryDate", typeof(DateTime));
                newTable.Columns.Add("StaffID", typeof(string));
                newTable.Columns.Add("MaterialPart", typeof(string));
                newTable.Columns.Add("DeliveryQuantity", typeof(float));
                newTable.Columns.Add("NotShipping", typeof(bool));
                newTable.Columns.Add("DeliveryDateTT", typeof(string));
                newTable.Columns.Add("InvoiceID", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));                
                gridF4MaterialDelivery.DataSource = newTable;
            }
            MaterialDelivery.Columns[0].Caption = "ID";
            MaterialDelivery.Columns[1].Caption = "Delivery Date";
            MaterialDelivery.Columns[2].Caption = "Staff ID";
            MaterialDelivery.Columns[3].Caption = "Material No.";
            MaterialDelivery.Columns[4].Caption = "Quantity";
            MaterialDelivery.Columns[5].Caption = "Not Shipping";
            MaterialDelivery.Columns[6].Caption = "Delivery Date TT";
            MaterialDelivery.Columns[7].Caption = "Invoice";            
            MaterialDelivery.Columns[8].Caption = "Remark";
            MaterialDelivery.Columns[0].Visible = false;
            MaterialDelivery.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F4MaterialDelivery add_Form = new frm_Add_F4MaterialDelivery();
            add_Form.ShowDialog();
            //LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void bottonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
